/**
 * @desc:用户注册页面
 * @author: zhoujin
  * @date: 2019-08-01
  * @update: 2019-08-01
  * @version: 1.0
  * @copyright: 2019-08-01
  * @remark:
 */
 <template>
   <div class="login">
    <div class="login-layout">
      <div class="form-container">
        <div class="title">我的后台管理系统</div>
        <el-form ref="form" :model="form" :rules="rules" >
          <el-form-item label="账号" prop="username">
            <el-input v-model="form.username" placeholder="请输入账号" ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" placeholder="请输入密码" ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="submit_btn" @click="onLogin('form')">登录</el-button>
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="form.remember" label="记住密码" border></el-checkbox>
          </el-form-item>

        </el-form>
      </div>
    </div>
   </div>
 </template>
 
 <script>
import { getCache, setCache, removeCache } from '@/utils/session';
 import { isPassWord } from '@/utils/validate';
 export default {
  name:'login',
  data() {
    return {
      userToken: '',//用户token
      form: {
        username:"zhoujin2@163.com",
        password:"223456",
        remember: true,//默认记住密码
      },
      rules:{
        username: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
        ],
        password:[
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    onLogin(form) {
      this.$refs[form].validate((valid) => {
        if(valid) {
          this.loading = true;
          const {username, password} = this.form
         
          
          // 调用了Vuex的dispatch方法来触发user模块中的LoginAction。
          this.$store.dispatch('user/LoginAction', {username, password})
          .then(() => {
            console.log('11111111111');
              // 记住密码：用户信息存sl、跳转到首页
              // 未记住：清除缓存
              if (this.form.remember) {
                setCache('LOGIN_INFO', { username, password })
                this.$router.push({
                  path: '/index'
                });
                console.log('登录成功-login');
                this.loading = false;
                // this.$message({
                //   message: '恭喜你，登录成功',
                //   type: 'success'
                // })
              } else {
                removeCache('LOGIN_INFO');
              }
            })
            .catch(err=> {
              setTimeout(() => {
                this.loading = false;
              }, 500);
            })
        }else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }





 }
 </script>
 
 <style lang="less" scoped>
  .login {
    height: 100vh;
    display: flex;
    .login-layout {
      flex:1;
      display: flex;
      justify-content: center;
      align-items: center;
      // TODO-bug: form内容超过容器
      .form-container {
        width: 400px;
        height: 400px;
      }
    }
  }
 </style>
 <style lang="less">
 .login {
  .login-layout {
    .form-container {
        .el-form {
          .el-form-item {
            margin-bottom: 5px !important;
          }
        }
        .submit_btn {
          width: 100%;
        }
      }
    }
  }
 </style>